package com.ruoyi.team.trash.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.result.report.domain.ResultReport;
import com.ruoyi.result.report.domain.vo.ResultReportVO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.team.trash.domain.AchievementTrash;
import com.ruoyi.team.trash.domain.vo.AchievementTrashVO;
import com.ruoyi.team.trash.mapper.AchievementTrashMapper;
import com.ruoyi.team.trash.service.IAchievementTrashService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 成果移除箱Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-24
 */
@Service
public class AchievementTrashServiceImpl implements IAchievementTrashService
{
    @Autowired
    private AchievementTrashMapper achievementTrashMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 查询成果移除箱
     *
     * @param id 成果移除箱主键
     * @return 成果移除箱
     */
    @Override
    public AchievementTrash selectAchievementTrashById(Long id)
    {
        return achievementTrashMapper.selectAchievementTrashById(id);
    }

    /**
     * 查询成果移除箱列表
     *
     * @param achievementTrash 成果移除箱
     * @return 成果移除箱
     */
    @Override
    public List<AchievementTrashVO> selectAchievementTrashList(AchievementTrash achievementTrash)
    {
        List<AchievementTrash> achievementTrashList = achievementTrashMapper.selectAchievementTrashList(achievementTrash);
        List<AchievementTrashVO> achievementTrashVOList = new ArrayList<>();

        for (AchievementTrash achievementTrash1 : achievementTrashList) {
            AchievementTrashVO achievementTrashVO = new AchievementTrashVO();
            BeanUtils.copyProperties(achievementTrash1, achievementTrashVO);

            // 根据 userId 查询 nick_name
            SysUser sysUser = sysUserMapper.selectUserById(achievementTrash1.getUserid());
            achievementTrashVO.setNickName(sysUser.getNickName());

            achievementTrashVOList.add(achievementTrashVO);
        }
        return achievementTrashVOList;
    }

    /**
     * 新增成果移除箱
     *
     * @param achievementTrash 成果移除箱
     * @return 结果
     */
    @Override
    public int insertAchievementTrash(AchievementTrash achievementTrash)
    {
        return achievementTrashMapper.insertAchievementTrash(achievementTrash);
    }

    /**
     * 修改成果移除箱
     *
     * @param achievementTrash 成果移除箱
     * @return 结果
     */
    @Override
    public int updateAchievementTrash(AchievementTrash achievementTrash)
    {
        return achievementTrashMapper.updateAchievementTrash(achievementTrash);
    }

    /**
     * 批量删除成果移除箱
     *
     * @param ids 需要删除的成果移除箱主键
     * @return 结果
     */
    @Override
    public int deleteAchievementTrashByIds(Long[] ids)
    {
        if (ids.length > 0) {
            for (Long id : ids) {
                AchievementTrash achievementTrash = achievementTrashMapper.selectAchievementTrashById(id);
                this.restoreAchievement(achievementTrash.getAchievementType(), achievementTrash.getAchievementId());
            }
        }
        return achievementTrashMapper.deleteAchievementTrashByIds(ids);
    }

    /**
     * 删除成果移除箱信息
     *
     * @param id 成果移除箱主键
     * @return 结果
     */
    @Override
    public int deleteAchievementTrashById(Long id)
    {
        return achievementTrashMapper.deleteAchievementTrashById(id);
    }

    @Override
    public int restoreAchievement(String achievementType, Long achievementId) {
        String tableName = getTableNameByType(achievementType);
        if (tableName == null) {
            throw new IllegalArgumentException("Invalid achievement type: " + achievementType);
        }

        String sql = "UPDATE " + tableName + " SET is_deleted = 0 WHERE id = ?";
        return jdbcTemplate.update(sql, achievementId);
    }

    private String getTableNameByType(String achievementType) {
        // 根据 achievement_type 返回对应的表名
        switch (achievementType) {
            case "档案资料":
                return "result_certificateinfo";
            case "继续教育材料":
                return "result_continuing_education_material";
            case "软件著作权":
                return "result_copyright";
            case "横向课题":
                return "result_horizontal_projects";
            case "纵向课题":
                return "result_longitudinal_projects";
            case "其它":
                return "result_other";
            case "论文":
                return "result_papers";
            case "专利":
                return "result_patents";
            case "个人获奖":
                return "result_personal_award";
            case "著作":
                return "result_publication";
            case "新闻报道":
                return "result_report";
            case "学生获奖":
                return "result_student_award";
        }
        return null;
    }

}
